using MediatR;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using Work.API.Applications.Read.Commands;
using Work.API.Applications.Write.Commands;
using Work.ErrorCode;

namespace Work.API.Controllers
{
    /// <summary>
    /// 处方管理控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PrescriptionController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<PrescriptionController> _logger;

        public PrescriptionController(IMediator mediator, ILogger<PrescriptionController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }

        /// <summary>
        /// 创建处方明细（单条）
        /// </summary>
        [HttpPost]
        public async Task<ApiResult<int>> Create(CreatePrescriptionItemCommand request)
        {
            try
            {
                return await _mediator.Send(request);
            }
            catch (System.Exception ex)
            {
                _logger.LogError(ex, "创建处方明细失败");
                return new ApiResult<int>
                {
                    Code = ApiEnum.失败,
                    Message = $"创建失败：{ex.Message}",
                    Data = 0
                };
            }
        }

        /// <summary>
        /// 批量创建处方明细
        /// </summary>
        [HttpPost]
        public async Task<ApiResult<int>> CreateBatch(CreatePrescriptionItemsCommand request)
        {
            try
            {
                return await _mediator.Send(request);
            }
            catch (System.Exception ex)
            {
                _logger.LogError(ex, "批量创建处方明细失败");
                return new ApiResult<int>
                {
                    Code = ApiEnum.失败,
                    Message = $"批量创建失败：{ex.Message}",
                    Data = 0
                };
            }
        }

        /// <summary>
        /// 更新处方明细
        /// </summary>
        [HttpPost]
        public async Task<ApiResult<int>> Update(UpdatePrescriptionItemCommand request)
        {
            try
            {
                return await _mediator.Send(request);
            }
            catch (System.Exception ex)
            {
                _logger.LogError(ex, "更新处方明细失败");
                return new ApiResult<int>
                {
                    Code = ApiEnum.失败,
                    Message = $"更新失败：{ex.Message}",
                    Data = 0
                };
            }
        }

        /// <summary>
        /// 删除处方明细
        /// </summary>
        [HttpPost]
        public async Task<ApiResult<int>> Delete(DeletePrescriptionItemCommand request)
        {
            try
            {
                return await _mediator.Send(request);
            }
            catch (System.Exception ex)
            {
                _logger.LogError(ex, "删除处方明细失败");
                return new ApiResult<int>
                {
                    Code = ApiEnum.失败,
                    Message = $"删除失败：{ex.Message}",
                    Data = 0
                };
            }
        }

        /// <summary>
        /// 根据预约ID获取处方明细列表
        /// </summary>
        [HttpGet]
        public async Task<ApiResult<System.Collections.Generic.List<Work.API.Dtos.PrescriptionItemDto>>> GetByAppointment(
            int appointmentId, 
            bool? unpaidOnly = null)
        {
            try
            {
                var query = new GetPrescriptionItemsByAppointmentQuery
                {
                    AppointmentId = appointmentId,
                    UnpaidOnly = unpaidOnly
                };
                return await _mediator.Send(query);
            }
            catch (System.Exception ex)
            {
                _logger.LogError(ex, "查询处方明细失败");
                return new ApiResult<System.Collections.Generic.List<Work.API.Dtos.PrescriptionItemDto>>
                {
                    Code = ApiEnum.失败,
                    Message = $"查询失败：{ex.Message}",
                    Data = new System.Collections.Generic.List<Work.API.Dtos.PrescriptionItemDto>()
                };
            }
        }
    }
}

